﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>添加会员</title>
    <script src="<%=Url.Content("~/Content/My97DatePicker/WdatePicker.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
    <link href="../../Content/Themes/blue2/css.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        *{ margin:0px; padding:0px;}
        #menu{  margin-left:20%; margin-top:20px;}
        .table { text-align:left;}
        .table tr td{ padding-left:15px; padding-top:10px;}
        .font{ color:Red; visibility:visible;}
        .borderOn{ border:solid 2px #1E90FF;}
        .borderOff{ border:1px solid #CCC;}
        .last{ margin-left:300px; }
        #reself,#cancel{ margin-left:20px;}
       
       
    </style>
    <script>
        // 清空打印缓存
        function PrintBufferClear() {
            var hComm = 0;

            hComm = GetCOM();

            vcp400.PrintBufferClear(hComm, 15);
            
            vcp400.ocx_CommClose(hComm);

        }
        function GetCOM() {
            COM = 100;
            Baud =57600;
            
            return vcp400.ocx_commopen(COM, Baud);
        }
        // 发送文本到缓冲区
        function SendText(content) {
           
            var hComm = 0;
            var sData = "";
            var textx = 0;
            var texty = 0;
           
            hComm = GetCOM();
            //PrintBufferClear();
            stext = content;
            textx = 0;
            texty = 50;
            
            nrtn = vcp400.SendText(hComm, 1, textx,texty, stext, 15); 
           // Print();
            vcp400.ocx_CommClose(hComm);
            
        }

        //进行打印或擦除
        function Print() {
            var hComm = 0;

            hComm = GetCOM();
                 
            vcp400.ErasePrintDischarge(hComm, 1, 1, 1, 15); 	// nMode=0, 不擦除，nMode=1 擦除 

            vcp400.ocx_CommClose(hComm);

        }
        // 退卡	
        function Dischange() {
            var hComm = 0;

            hComm = GetCOM();

            vcp400.Dischange(hComm, 1, 10); 	// 0=不完全退卡,1=完全退卡

            vcp400.ocx_CommClose(hComm);

        }
        // 写指定块数据
        function RfidCardWriteData() {
            var hComm = 0;
            hComm = GetCOM();
            var memberCardId = document.getElementById("memberCardId").value;
            nrtn = vcp400.RfidCardWriteData(hComm, 1, 0, "", memberCardId, 15);
            if (nrtn = 32) {
                
            }
            else {
                alert('写数据失败.')
            }
            vcp400.ocx_CommClose(hComm);
        }
        // 获取机器状态	
        function RequestStatus() {
            var hComm = 0; 
            hComm = GetCOM(); 
            sState = vcp400.RequestStatus(hComm, 1); 
            if (sState.length = 6) {
                nchar = sState.charAt(0);
                switch (parseInt(nchar)) {
                    case 1: return 1;
                     break;
                } 
                nchar = sState.charAt(3);
                switch (parseInt(nchar)) {
                    case 1: return 1; break; 
                } 
            } 
            vcp400.ocx_CommClose(hComm); 
        }	
    </script>
</head>
<body class="main">

 <div   class="AllWebTopDiv">
    <div class="path">
             <div class="nav">
               <a href="#">添加会员</a>
             </div>
        </div>
       <div class="content">
    <div id="menu">
        <table class="table">
          <tr>
             <td>
             <label>会员编号:</label>
             <input  type="text" id="memberId" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'" value="<%:ViewData["memberId"] %>" readonly="readonly"/> 
             <input  type="hidden" id="memberCardId" value="<%:ViewData["memberCardId"]%> "/>
             </td>
             <td class="style1">
             <label>会员姓名:</label>
             <input  type="text" id="memberName" onblur="checkAll(this);"  onfocus="this.className='borderOn'"/><span id="checkName" class="font">*</span>        
             </td>
             <td class="style1">
             <label>卡片类型:</label>
              <%:Html.DropDownList("cardType")%>
             </td>
          </tr>
          <tr>
             <td>
             <label>卡片密码:</label>
             <input  type="password" id="pwd" onfocus="this.className='borderOn'"  onblur="pwd()" /><span id="show_pwd" class="font">*</span>       
             </td>
             <td>
             <label>确认密码:</label>
             <input  type="password" id="pwd1" onfocus="this.className='borderOn'"  onblur="confirm_pwd()" /><span id="con_pwd" class="font">*</span>        
             </td>
             <td>
             <label>手机号码:</label>
             <input  type="text" id="mobile" onblur="checkNaN();" onfocus="this.className='borderOn'"/><span id="checkMobile" class="font">*</span>      
             </td>
          </tr>
          <tr>
             <td>
             <label>会员性别:</label>
             <select id="sex">
              <option >男</option>
              <option>女</option>
             </select>      
             </td>
             <td>
              &nbsp;&nbsp;&nbsp;<label>Email:</label>
             <input  type="text" id="email"  onfocus="this.className='borderOn'"  onblur="checkAll(this);"/> <span id="checkEmail" class="font">*</span>          
             </td>
             <td>
             <label>身份证号:</label>
              <input  type="text" id="numberID" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'" />   
             </td>
          </tr>
          <tr>
             <td>
             <label>联系电话:</label>
             <input  type="text" id="tel" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'" />          
             </td>
             <td>
             <label>出生日期:</label>
             <input class="Wdate" type="text" id="birthday" onclick="WdatePicker()" onblur="checkAll(this);"onfocus="this.className='borderOn'" /><span id="checkBirthday" class="font">*</span>         
             </td>
             <td>
             <label>当前状态:</label>
             <select id="status">
              <option value="0" >正常</option>
              <option value="1">挂失</option>
              <option value="2">停用</option>
             </select>
             </td>
          </tr>
            <tr>
             <td>
             <label>截止日期:</label>
             <input class="Wdate" type="text" id="expiresDate" onclick="WdatePicker()" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/>      
             </td>
             <td>
             <label>登记日期:</label>
             <input class="Wdate" type="text" id="createDate" onclick="WdatePicker()" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/>          
             </td>
             <td>
             <label>婚姻状况:</label>
             <select id="marriage">
              <option>已婚</option>
              <option>未婚</option>
             </select>
             </td>
          </tr>
            <tr>
             <td>
             &nbsp;&nbsp;&nbsp;&nbsp;<label class="chang">区域:</label>
               <select id="areaName">
                 <% for (int i = 0; i < Model.Count; i++)
                    {%>

                      <option value="<%:Model[i].id %>"><%:Model[i].AreaName%></option>
                 <%} %>
               </select>      
             </td>
             <td>
             <label>邮政编码:</label>
                <input  type="text" id="zip" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/>       
             </td>
             <td>
             <label>通讯地址:</label>
              <input  type="text" id="address" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/> 
             </td>
          </tr>
            <tr>
             <td class="style1">
             <label>会员类型:</label>
             <input  type="text" id="memberType" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/>          
             </td>
             <td colspan="2" class="style1">
              &nbsp;&nbsp;<label>接待人:</label>
            <input  type="text" id="tuiguangren" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/>    
             </td>
             
            </tr>
             <tr>           
             <td colspan="3">
                 &nbsp;&nbsp;&nbsp;&nbsp;<label>备注:</label> 
              <input  type="text" id="memo" onfocus="this.className='borderOn'"  onblur="this.className='borderOff'"/>
             </td>    
             </tr>
             <tr >
               <td class="last"></td>
               <td><input type="button" id="sure" value="确定"  onclick="save()"  class="button2" />
               <input type="button" id="reself" value="重填" onclick="clearInput();" class="button2" /> 
               <input type="button" id="cancel" value="取消"  onclick="closeWindows();" class="button2"/>
               </td>
             </tr>
        </table>
    </div>
  </div>
  <p>
  <object classid="clsid:2323E9E1-2B2E-4BD2-9F66-1C2D02F7A29C" codebase="vcp400.cab" width="0" height="0" id="vcp400">
  </object>
</p>
</div>
</body>
</html>
<script type="text/javascript">
  var n=false;
  var p=false;
  var cp=false;
  var numX=false;
  var e=false;
  var b=false;
   function checkAll(o)   //判断是否为空
    {
    //解决IE中不识别trim()
      if (!String.prototype.trim) {
            var TRIM_REG = /(^\s*)|(\s*$)/g;
            String.prototype.trim = function () { return this.replace(TRIM_REG, ''); }
        }
                  var thisvar=o.value.trim();

                  if(o.id=='memberName')
                  {                 
                     if(thisvar==null ||thisvar.length==0)
                    {
                     $("#checkName").css("visibility", "visible");
                     $("#sure").attr("disabled", "false");
                     o.className="borderOff";
                     }
                     else
                     {
                     $("#checkName").css("visibility", "hidden");
                     $("#sure").removeAttr("disabled");
                     o.className="borderOff";
                     n=true;
                     }

                  }
                  if(o.id=='birthday')
                  {
                   if(thisvar==null ||thisvar.length==0)
                    {
                     $("#checkBirthday").css("visibility", "visible");
                     $("#sure").attr("disabled", "false");
                     o.className="borderOff";
                     }
                     else
                     {
                     $("#checkBirthday").css("visibility", "hidden");
                      $("#sure").removeAttr("disabled");
                      o.className="borderOff";
                      b=true;
                     }
                  } 
                  if(o.id=='email')
                  {
                   if(thisvar==null ||thisvar.length==0)
                    {
                     $("#checkEmail").css("visibility", "visible");
                     $("#sure").attr("disabled", "false");
                     o.className="borderOff";
                     }
                     else
                     {
                     $("#checkEmail").css("visibility", "hidden");
                      $("#sure").removeAttr("disabled");
                      o.className="borderOff";
                      e=true;
                     }
                    // var sReg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
                    // if(!sReg.test(o.value))
                   //  {
                    //  $("#checkEmail").css("visibility", "visible");
                    // $("#sure").attr("disabled", "false");
                   //  o.className="borderOff";
                   //e=false;
                    // }
                    //  else
                    // {
                    // $("#checkEmail").css("visibility", "hidden");
                    //  $("#sure").removeAttr("disabled");
                    //  o.className="borderOff";
                    //  e=true;
                    // }
                  }                             
                  
    }
  //判断手机号码是否是整数
    function checkNaN() {
        if (!String.prototype.trim) {
            var TRIM_REG = /(^\s*)|(\s*$)/g;
            String.prototype.trim = function () { return this.replace(TRIM_REG, ''); }
        }
        var m = document.getElementById('mobile').value.trim(); 
        if (m.length !=0) {
            reg = /^[-+]?\d*$/;
            if (!reg.test(m)) {
                $("#checkMobile").css("visibility", "visible");
                $("#sure").attr("disabled", "false");
                $("#mobile").addClass("borderOff");
                numX=false;
            }
            else {
                $("#checkMobile").css("visibility", "hidden");
                 $("#sure").removeAttr("disabled");
                 $("#mobile").addClass("borderOff");
                 numX=true;
            }
         }

        else {

              if(m.length==0)
              {
               $("#checkMobile").css("visibility", "visible");
               $("#sure").attr("disabled", "false");
               $("#mobile").addClass("borderOff");
               numX=false;
              }
              else
              {
                 $("#checkMobile").css("visibility", "hidden");
                  $("#sure").removeAttr("disabled");
                  $("#mobile").addClass("borderOff");
                   numX=true;
              }
          
          }

    }
     function pwd(){
        if (!String.prototype.trim) {
            var TRIM_REG = /(^\s*)|(\s*$)/g;
            String.prototype.trim = function () { return this.replace(TRIM_REG, ''); }
        }
         var m = document.getElementById('pwd').value.trim(); 
        if (m.length !=0) {
            reg = /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{3,22}$/;
            if (!reg.test(m)) {
                $("#show_pwd").css("visibility", "visible");
                $("#sure").attr("disabled", "false");
                $("#pwd").addClass("borderOff");
                p=false;
            }
            else {
                $("#show_pwd").css("visibility", "hidden");
                 $("#sure").removeAttr("disabled");
                 $("#pwd").addClass("borderOff");
                 p=true;
            }
         }

        else {

              if(m.length==0)
              {
               $("#show_pwd").css("visibility", "visible");
               $("#sure").attr("disabled", "false");
               $("#pwd").addClass("borderOff");
               p=false;
              }
              else
              {
                 $("#show_pwd").css("visibility", "hidden");
                  $("#sure").removeAttr("disabled");
                  $("#pwd").addClass("borderOff");
                  p=true;
              }
          
          }
     }
      function confirm_pwd(){
         if (!String.prototype.trim) {
            var TRIM_REG = /(^\s*)|(\s*$)/g;
            String.prototype.trim = function () { return this.replace(TRIM_REG, ''); }
        }
         var m = document.getElementById('pwd').value.trim(); 
         var con_pwd=document.getElementById('pwd1').value.trim(); 
        if (m !=con_pwd) {
               
                $("#con_pwd").css("visibility", "visible");
                 $("#sure").attr("disabled", "false");
                 $("#pwd1").addClass("borderOff"); 
                 cp=false;
         }

        else {
          
                 $("#con_pwd").css("visibility", "hidden");
                  $("#sure").removeAttr("disabled");
                  $("#pwd1").addClass("borderOff");
                  cp=true; 
          }
      }
    function save() {
        
        if(n==false || numX==false || p==false || cp==false|| e==false || b==false){
         alert("必填项有误");
         return false;
        } 
        var memberId = document.getElementById("memberId").value;
        var memberName = document.getElementById("memberName").value;
        var memberCardId = document.getElementById("memberCardId").value;
        var index = document.getElementById("cardType").selectedIndex;
        var cardType = document.getElementById("cardType").options[index].value;
        var pwd = document.getElementById("pwd").value;
        var pwd1 = document.getElementById("pwd1").value;
        var mobile = document.getElementById("mobile").value;
        var sexindex = document.getElementById("sex").selectedIndex
        var sex = document.getElementById("sex").options[sexindex].text;
        var email = document.getElementById("email").value;
        var numberID=document.getElementById("numberID").value;
        var tel = document.getElementById("tel").value;
        var birthday = document.getElementById("birthday").value;
        var statusindex = document.getElementById("status").selectedIndex
        var status = document.getElementById("status").options[statusindex].value;
        var expiresDate = document.getElementById("expiresDate").value;
        var createDate = document.getElementById("createDate").value;
        var marriageindex = document.getElementById("marriage").selectedIndex;
        var marriage = document.getElementById("marriage").options[marriageindex].text;
        var areaNameIndex=document.getElementById("areaName").selectedIndex;
        var areaName = document.getElementById("areaName").options[areaNameIndex].value;
        var zip = document.getElementById("zip").value;
        var address = document.getElementById("address").value;
        var memberType = document.getElementById("memberType").value;
        var tuiguangren = document.getElementById("tuiguangren").value;
        var memo = document.getElementById("memo").value;
        var content= memberName+"\n"+cardType+"\n"+tuiguangren+"\n"+ address;
        var print=false;
        if( confirm("是否打印信息？"))
        {
                print=true;
        }
      $.ajax({
            type:"POST",
            url:"<%:Url.Action("Member","MemberAdd") %>",
            data:"memberId="+memberId+"&memberCardId="+memberCardId+"&memberName="+memberName+"&cardType="+cardType+"&pwd="+pwd+"&pwd1="+pwd1+"&mobile="+mobile+"&sex="+sex+"&email="+email+"&numberID="+numberID+"&tel="+tel+"&birthday="+birthday+"&status="+status+"&expiresDate="+expiresDate+"&createDate="+createDate+"&marriage="+marriage+"&areaName="+areaName+"&zip="+zip+"&address="+address+"&memberType="+memberType+"&tuiguangren="+tuiguangren+"&memo="+memo+"&print="+print+"",
            cache:false,
            success:function(txt)
            {
             
                 
                if(txt!="添加成功" && txt!="添加失败"&& txt!="两次输入密码不一致" &&txt!="必填项忘填了" ){ 
                 if(RequestStatus()==1){
                    PrintBufferClear();
                    RfidCardWriteData();
                     SendText(txt);
                     Print(); 
                 }
                 else{
                   alert("未检测到卡,添加成功");
                    window.location.href="/MemberAdd/MemberAdd";
                    return false;
                 }
                 alert("添加成功");
                 window.location.href="/MemberAdd/MemberAdd";
                }
                else{ 
                
                  if(txt=="添加成功"){
                 if(RequestStatus()==1){
                   RfidCardWriteData();
                   Dischange();
                  }
                    window.location.href="/MemberAdd/MemberAdd";
                  }
                    alert(txt);
                }
     
            }  
        })
    }
    //清空文本框
    function  clearInput()
    {
      var txts=document.getElementsByTagName("input"); 
     for(var i=0;i <txts.length;i++) 
     { 
      if(txts[i].type=="text") 
      { 
       txts[i].value =""; 
      } 
     }
    } 
    //关闭窗口
    function  closeWindows()
    { 
         window.location.href = "/Loading/Index/?src=/Desktop/Index/";
     
    }
</script>
